<?php
   include_once dirname(__FILE__)."/../include/GenerateSql.php";
	include_once dirname(__FILE__)."/../include/Constante.php";
	include_once dirname(__FILE__)."/../include/Functions.php";
	
	class Person
	{
		private $gSql;
		private $msgErro;
		
		public $id;
		public $user_name;
		public $user_pass;
		public $facebook_id;
		public $facebook_token;
		public $first_name;
		public $last_name;
		public $email;
		public $gender;
		public $time_zone;
		public $locale;
		public $strava_id;
		public $strava_token;
		public $age;
		
		public function __construct()
		{
		   $this->gSql = new GenerateSql();		
		}

		public function __destruct()
		{
		}
		
		public function getMsgErro()
		{
		   return $this->msgErro;
		}
		
		private function store($type)
		{		   
		   $this->gSql->prepare();
			
			if( $type == "I")
				$this->gSql->setCommandType(Constante::CMD_INSERT);
			else
			{
				$this->gSql->setCommandType(Constante::CMD_UPDATE);
				$this->gSql->setValue($this->id,"id");
			}
			
			$this->gSql->setTable("person");
			
			$this->gSql->setValue($this->user_name,"user_name");
			$this->gSql->setValue($this->user_pass,"user_pass");
			$this->gSql->setValue($this->facebook_id,"facebook_id");
			$this->gSql->setValue($this->facebook_token,"facebook_token");
			$this->gSql->setValue($this->first_name,"first_name");
			$this->gSql->setValue($this->last_name,"last_name");
			$this->gSql->setValue($this->email,"email");
			$this->gSql->setValue($this->gender,"gender");
			$this->gSql->setValue($this->time_zone,"time_zone");
			$this->gSql->setValue($this->locale,"locale");
			$this->gSql->setValue($this->strava_id,"strava_id");
			$this->gSql->setValue($this->strava_token,"strava_token");
			$this->gSql->setValue($this->age,"age");
			
			if( !$this->gSql->execute())
			{
			   $this->msgErro = $this->gSql->getMsgErro();
				return false;
			}
			if( $type == "I")
				$this->id = Functions::getValue("SELECT MAX(id) FROM person");			
			return true;	
		}
		
		public function set($array)
		{
			$this->id = $array["id"];
			$this->user_name = $array["user_name"];
			$this->user_pass = $array["user_pass"];
			$this->facebook_id = $array["facebook_id"];
			$this->facebook_token = $array["facebook_token"];
			$this->first_name = $array["first_name"];
			$this->last_name = $array["last_name"];
			$this->email = $array["email"];
			$this->gender = $array["gender"];
			$this->time_zone = $array["time_zone"];
			$this->locale = $array["locale"];
			$this->strava_id = $array["strava_id"];
			$this->strava_token = $array["strava_token"];
			$this->age = $array["age"];
		}
		
		public function get()
		{
			$array = Functions::getArraySql("SELECT * FROM person WHERE id = ".$this->status_id);
			$this->set($array);
		}		
		
		public function getByFaceId($facebook_id)
		{
			$array = Functions::getArraySql("SELECT * FROM person WHERE facebook_id = ".$facebook_id);
			//var_dump($array);
			if( count($array) != 0){
				$this->set($array);
				return true;
			}
			return false;
		}
		
		public function save()
		{
		   return $this->store("I");
		}
		
		public function update()
		{
			return $this->store("U");
		}
		
		public function delete()
		{
		
		}
	}
?>